package com.apache.funds.controller;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.apache.api.api.ApiService;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.database.constant.Validator;
import com.apache.rpc.util.JsonEntityParese;
import com.apache.rpc.util.XMLMapParse;

/**
 * description: Http或Https访问action
 * 
*/
@Controller
@RequestMapping("funds/service/")
public class RestfullFundsAction {
	@Autowired
	private ApiService fundsService;

	@RequestMapping("doService.action")
	public void doService(HttpServletRequest request, HttpServletResponse response) {
		String result = "";
		try {
			ParamsVo vo = new ParamsVo();
			String paramStr = request.getParameter("params");
			String paramType = request.getParameter("ParamType");
			Map<String, Object> headMap = new HashMap<String, Object>();
			Map<String, Object> bodyMap = new HashMap<String, Object>();
			XMLMapParse.instance().xmlGetData(paramStr, headMap, bodyMap);
			vo.setMethodKey(headMap.get("MethodCode").toString());
			vo.setParams(bodyMap);
			ResultEntity entity = fundsService.doService(vo);
			if (Validator.isEmpty(entity)) {
				entity = new ResultEntity();
				entity.setResult("false");
				entity.setMessage("接口调用失败！");
			}
			if ("json".equalsIgnoreCase(paramType))
				result = JsonEntityParese.instance().beanToJson(entity);
			else
				result = JsonEntityParese.instance().toXml(entity);
			response.setContentType("text/html; charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print(result);
			out.flush();
		} catch (Exception e) {
		}
	}
}
